package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.pay.OnMyPayListner;
import com.trouble.framework.pay.OnPayListener;
import com.trouble.framework.pay.PayType;
import com.trouble.framework.pay.alipay.Alipay;
import com.trouble.framework.pay.wechat.WechatPay;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.app.SysApplication;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.model.PayOrderInfo;
import com.tiantianyuegou.pay.AccountPay;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.view.MyDialogTextView;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * 收银台界面
 * Created by ivany on 2016/1/14.
 */
public class PayActivity extends BaseActivity implements View.OnClickListener, OnPayListener, OnMyPayListner {

//六个界面都会跳转到这个界面，通过类型来进行判断
    private ImageView mImg_balance, mImg_weChat, mImg_zhiFuBao, mImg_unionPay;
    private LinearLayout mLinear_balance, mLinear_weChat, mLinear_zhiFuBao, mLinear_unionPay;
    private TextView mTxt_value;

    private LoadingDialog mLoadingDialog;
    private String orderNo;
    private String orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        setTitle("收银台");
        Tools.changeSystemBar(PayActivity.this);
        initViews();
        //将activity加入栈
        SysApplication.getInstance().addActivity(this);
        SysApplication.getInstance().payInit();
    }

    @Override
    public void findViewByIds() {
        mImg_balance = (ImageView) findViewById(R.id.pay_balance_img);
        mImg_weChat = (ImageView) findViewById(R.id.pay_wechat_img);
        mImg_zhiFuBao = (ImageView) findViewById(R.id.pay_zhifubao_img);
        mImg_unionPay = (ImageView) findViewById(R.id.pay_unionpay_img);
        mLinear_balance = (LinearLayout) findViewById(R.id.pay_balance);
        mLinear_weChat = (LinearLayout) findViewById(R.id.pay_wechat);
        mLinear_zhiFuBao = (LinearLayout) findViewById(R.id.pay_zhifubao);
        mLinear_unionPay = (LinearLayout) findViewById(R.id.pay_unionpay);
        mTxt_value = (TextView) findViewById(R.id.pay_value);
    }

    @Override
    public void initListener() {
        mLinear_balance.setOnClickListener(this);
        mLinear_weChat.setOnClickListener(this);
        mLinear_zhiFuBao.setOnClickListener(this);
        mLinear_unionPay.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mLoadingDialog = new LoadingDialog(this);
        String value = getIntent().getStringExtra("value");
        orderNo = getIntent().getStringExtra("orderNo");
        orderId = getIntent().getStringExtra("orderId");
        mTxt_value.setText(value);
    }

    @Override
    public void onClick(View v) {
        cancelAll();


        switch (v.getId()) {
            case R.id.pay_balance:
                mImg_balance.setImageResource(R.mipmap.select_icon);
                checkPassWord();
                break;
            case R.id.pay_wechat:
                //Alipay.getInstance().pay(PayActivity.this,orderInfo,PayActivity.this);
                mImg_weChat.setImageResource(R.mipmap.select_icon);
                break;
            case R.id.pay_zhifubao:
                Log.e("TAG", "alipay start");
                com.trouble.framework.pay.PayOrderInfo orderInfo=new com.trouble.framework.pay.PayOrderInfo();
                orderInfo.setOrderNo(orderNo);
//                orderInfo.setGoodsPrice(mTxt_value.getText().toString());
                orderInfo.setGoodsPrice("0.01");
                orderInfo.setGoodsName("测试001");
                orderInfo.setGoodsDesc("这是测试");
                Alipay.getInstance().pay(this, orderInfo, this);
                mImg_zhiFuBao.setImageResource(R.mipmap.select_icon);
                break;
            case R.id.pay_unionpay:
                mImg_unionPay.setImageResource(R.mipmap.select_icon);
                break;
        }
    }

    //检查是否设置了余额支付密码
    private void checkPassWord() {
        mLoadingDialog.setMsg("正在确认订单");
        mLoadingDialog.show();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        http://hjtech.wicp.net:8080/tiantianyuegou-api/one/pay?tmId=1216&score=111&paypass=e10adc3949ba59abbe56e057f20f883e
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.CHECK_PAY_PASS, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "cheek PassWord response:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("检查支付密码失败");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            AccountPay pay = new AccountPay(1);
                            PayOrderInfo orderInfo = new PayOrderInfo();
                            orderInfo.setOrderID(orderId);
                            orderInfo.setOrderNo(orderNo);
                            orderInfo.setGoodsName("测试001");
                            orderInfo.setGoodsDesc("这是测试");
                            orderInfo.setGoodsPrice(mTxt_value.getText().toString());
                            pay.pay(PayActivity.this, orderInfo, PayActivity.this);
                        } else {
                            MyDialogTextView dialog = new MyDialogTextView(PayActivity.this, "提示", "您还未设置支付密码，为保护账户安全，请先设置支付密码!");
                            dialog.setDialogCallback(new MyDialogTextView.DialogCallback() {
                                @Override
                                public void dialogOpt() {
                                    Intent intent = new Intent(PayActivity.this, SettingBalanceActivity.class);
                                    startActivityForResult(intent, Constant.SETTING_BALANCE_REQUEST);
                                }
                            });
                            dialog.show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "request failure :" + s);
                showMsg("当前网络不可用，请检查您的网设置");
            }
        });
    }

    private void cancelAll() {
        mImg_balance.setImageResource(R.mipmap.unselect_icon);
        mImg_weChat.setImageResource(R.mipmap.unselect_icon);
        mImg_zhiFuBao.setImageResource(R.mipmap.unselect_icon);
        mImg_unionPay.setImageResource(R.mipmap.unselect_icon);
    }

    @Override
    public void onPayStart(PayType payType) {
        switch (payType) {
            case ALIPAY:
                Intent intent = new Intent(this, PaySuccessActivity.class);
                intent.putExtra("value", mTxt_value.getText().toString());
                intent.putExtra("orderId", orderId);
                break;
            case WECHAT:
                break;
        }
    }

    @Override
    public void onPaySuccessed(PayType payType, String s) {
        switch (payType) {
            case ALIPAY:

                break;
            case WECHAT:
                break;
        }
    }

    @Override
    public void onPayFailed(PayType payType, String s) {
        switch (payType) {
            case ALIPAY:
                showMsg("支付失败");
                break;
            case WECHAT:
                break;
        }
    }

    @Override
    public void onPayCanceled(PayType payType) {
        switch (payType) {
            case ALIPAY:
                showMsg("支付取消");
                break;
            case WECHAT:
                break;
        }
    }

    @Override
    public void onPayConfirming(PayType payType) {
        switch (payType) {
            case ALIPAY:
                showMsg("正在确认订单...");
                break;
            case WECHAT:
                break;
        }
    }

    @Override
    public void onPayStart(com.tiantianyuegou.enums.PayType var1) {

    }

    @Override
    public void onPaySuccessed(com.tiantianyuegou.enums.PayType var1, String var2) {

    }

    @Override
    public void onPayFailed(com.tiantianyuegou.enums.PayType var1, String var2) {

    }

    @Override
    public void onPayCanceled(com.tiantianyuegou.enums.PayType var1) {

    }

    @Override
    public void onPayConfirming(com.tiantianyuegou.enums.PayType var1) {

    }
}
